﻿/*
* Project: HOERP
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2011年5月14日 星期六 凌晨 0:45:04
*/

package com.serp.act.xt;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;
import org.tulip.DaoException;

import com.serp.Constants;
import com.serp.action.BaseAct;
import com.serp.business.xt.CustomType;
import com.serp.business.xt.CustomTypeDao;
import com.serp.form.xt.CustomTypeForm;

import com.serp.web.ec.ECConstants;

/**
 * 客户类别Action
 */
public class CustomTypeAct extends BaseAct {
	
	@Override
	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - start"); //$NON-NLS-1$
		CustomTypeForm customTypeForm = (CustomTypeForm) form;
		if (null != customTypeForm) {
		 				
			// 新增
			if (Constants.ACT_ADD.equals(customTypeForm.getAct())) {
				if (isTokenValid(request)) {
					addCustomType(customTypeForm);
					setFormNull(customTypeForm);
				} else {
					setFormNull(customTypeForm);
					customTypeForm.setMessages("请不要重复提交!");
				}
			}

			// 删除
			else if (Constants.ACT_DEL.equals(customTypeForm.getAct())) {
				delCustomType(customTypeForm);
				setFormNull(customTypeForm);
			}
			// 编辑
			else if (Constants.ACT_EDIT.equals(customTypeForm.getAct())) {
				editCustomType(customTypeForm);
				customTypeForm.setAct(Constants.ACT_UPDATE);
			}
			// 编辑后保存--更新
			else if (Constants.ACT_UPDATE.equals(customTypeForm.getAct())) {
				if (isTokenValid(request)) {
					updateCustomType(customTypeForm);
					//setFormNull(customTypeForm);
				} else {
					setFormNull(customTypeForm);
					customTypeForm.setMessages("请不要重复提交!");
				}

			}
			//默认显示
			else {
				setFormNull(customTypeForm);
			}

		}

		// 显示列表
		displayTable(request);
		
		ActionForward returnActionForward = mapping.findForward("success");

		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end"); 
		return returnActionForward;
	}
	


	/**
	 * 显示客户类别列表
	 */
	private void displayTable(HttpServletRequest request) throws DaoException {
		CustomTypeDao dao = new CustomTypeDao();
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		Limit limit = new TableLimit(limitFactory);
		int totalRows = dao.getTotalCustomTypes();
		limit.setRowAttributes(totalRows,
				ECConstants.PAGE_DEFAULT_ROWS_DISPALAY);

		Sort sort = limit.getSort();
		String prop = sort.getAlias();
		String sortOrder = sort.getSortOrder();
		List customTypes = dao.getCustomTypes(prop, sortOrder, limit.getCurrentRowsDisplayed(), limit.getRowStart());
		request.setAttribute("totalRows", new Integer(totalRows));
		request.setAttribute("CustomTypes", customTypes);
	}

    /**
	 * 更新客户类别
	 */
	private void updateCustomType(CustomTypeForm customTypeForm) {
		CustomType customType = new CustomType();
		try {
			BeanUtils.copyProperties(customType, customTypeForm);
			new CustomTypeDao().updateCustomType(customType);
		} catch (Exception e) {
			customTypeForm.setMessages("更新客户类别失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 编辑客户类别
	 */
	private void editCustomType(CustomTypeForm customTypeForm) {
		try {
			CustomType obj = new CustomTypeDao().getCustomTypeByID(customTypeForm.getCustTypeId());
			
	        customTypeForm.setCustTypeId(obj.getCustTypeId());
	        customTypeForm.setCustTypeName(obj.getCustTypeName());
	        customTypeForm.setCustMateDisc(obj.getCustMateDisc());
	        customTypeForm.setProcDisc(obj.getProcDisc());
	        customTypeForm.setCustTypeRema(obj.getCustTypeRema());
	        customTypeForm.setCustType(obj.getCustType());
						
		} catch (DaoException e) {
			customTypeForm.setMessages("查询客户类别失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 初始化（置空）客户类别
	 */
	private void setFormNull(CustomTypeForm customTypeForm) {
		//customTypeForm.setAct(Constants.ACT_ADD);
		
	    customTypeForm.setCustTypeId(0);
	    customTypeForm.setCustTypeName(null);
	    customTypeForm.setCustMateDisc(0.0);
	    customTypeForm.setProcDisc(0.0);
	    customTypeForm.setCustTypeRema(null);
	    customTypeForm.setCustType(0);
		
		customTypeForm.setMessages(null);
	}

	/**
	 * 删除客户类别
	 */
	private void delCustomType(CustomTypeForm customTypeForm) {
		CustomType customType = new CustomType();
		try {
			BeanUtils.copyProperties(customType, customTypeForm);
			new CustomTypeDao().delCustomType(customType);
		} catch (Exception e) {
			customTypeForm.setMessages("删除客户类别失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 添加客户类别
	 */
	private void addCustomType(CustomTypeForm customTypeForm){
		CustomType customType = new CustomType();
		try {
			BeanUtils.copyProperties(customType, customTypeForm);
			new CustomTypeDao().addCustomType(customType);
		} catch (Exception e) {
			customTypeForm.setMessages("添加客户类别失败!" + e.getMessage());
			e.printStackTrace();
		}
	}


}